﻿<%@ Control Language="C#" Inherits="Natrue.Web.MVC.AuthViewUserControl" %>
<%@ Import Namespace="Natrue.Popedom.Organizational"%>

<%
    Response.Write(BuildParentSelectControl());
     %>

<script type="text/C#" runat="server">
    
    private string BuildParentSelectControl()
    {
        IOrganizationalManager mgr = ProxyContainer.FindLocalProxy<IOrganizationalManager>();
        const string sele = "<select id=\"{1}\" name=\"{1}\" class=\"selectOption\" style=\"width:200px;\">{0}</select>";
        string reval = "";
        IList<DepartmentBase> rootDepartments = mgr.GetRootDepartment();
        foreach (DepartmentBase department in rootDepartments)
            reval += BuildItems(department, String.Empty);
        return String.Format(sele, reval,ViewData["departname"]??"smDepartment");
    }

    private string BuildItems(DepartmentBase department, string flow)
    {
        string reVal = String.Format("<option value=\"{0}\" {2}>{1}</option>", department.PrimaryKey,
                                     flow + department.DepartmentName,
                                     (ViewData["selectkey"] + "" ?? "").Equals(department.PrimaryKey.ToString())
                                         ? "selected=\"selected\"" : "");
        IOrganizationalManager mgr = ProxyContainer.FindLocalProxy<IOrganizationalManager>();
        IList<DepartmentBase> rootDepartments = mgr.GetChildDepartment(department.PrimaryKey);
        foreach (DepartmentBase departmentBase in rootDepartments)
            reVal += BuildItems(departmentBase, flow + "  ┣");
        return reVal;
    }

</script>

